<extend name="Extends/base"/>
<block name="title">用户管理</block>
<block name="css">
</block>
<block name="body_style">class="bgc-fff"</block>
<block name="body">
    <div class="pt30 plr20">
        <form id='myForm'>
            <div class="form-layout form-inline">
                <div class="control-group">
                    <label class="control-label c-t-right">手机号：</label>
                    <div class="control-form">
                        <input type="text" class="input-text" name="phone"
                               placeholder="请输入手机号搜索" >
                    </div>
                </div>

                <div class="control-group">
                    <label class="control-label c-t-right">姓名：</label>
                    <div class="control-form">
                        <input type="text" class="input-text" name="name"
                               placeholder="请输入手机号搜索" >
                    </div>
                </div>

                <div class="control-group">
                    <a href="javascript:;" class="search-btn c-btn c-btn-blue mr10">查询用户</a>
                    <a href="javascript:;"
                       class="add-user c-btn c-btn-green mr10">新增用户</a>
                </div>
            </div>
        </form>

        <div class="c-nodata pt100 c-t-center">
            <img src="__PC__/images/nodata.png">
            <p class="pt30 c-f14 msg"></p>
        </div>

        <div id="myList" class="c-hide">
        </div>
    </div>
</block>
<block name="js">

    <script type="text/javascript">
        $(function () {
            getPigeonList();
        });

        $(document).on("click", ".search-btn", function () {
            getPigeonList();
        })

        /**
         * 添加用户
         */
        $(document).on("click", ".add-user", function () {
            doGetAddUserForm(function (res) {
                if (res.Code != 10000) {
                    $.toast(res.Message);
                    return false;
                }

                var html = res.Result.html;
                var idx = $.alert(html, {
                    "title": '添加用户',
                    "ok": false,
                    "init": function () {
                        $("#addForm .close-btn").on("click", function () {
                            idx.close();
                        });

                        $("#addForm .submit-btn").on("click", function () {
                            var data = getFormData($("#addForm"));
                            doAddUser(data, function (res) {
                                if (res.Code != 10000) {
                                    $.toast(res.Message);
                                    return false;
                                }
                                idx.close();
                                $.toast("添加成功", 800, function () {
                                    getPigeonList();
                                })
                            });
                        });
                    }
                });
            });
        });


        /**
         * 编辑用户
         */
        $(document).on("click", ".edit-user", function () {
            var id = $(this).attr("data-id");
            if (!id) {
                $.toast("编辑ID不能为空");
                return false;
            }
            doGetEditUserForm(id, function (res) {
                if (res.Code != 10000) {
                    $.toast(res.Message);
                    return false;
                }

                var html = res.Result.html;
                var idx = $.alert(html, {
                    "title": '编辑用户',
                    "ok": false,
                    "init": function () {
                        $("#editForm .close-btn").on("click", function () {
                            idx.close();
                        });

                        $("#editForm .submit-btn").on("click", function () {
                            var data = getFormData($("#editForm"));
                            doEditUser(id, data, function (res) {
                                if (res.Code != 10000) {
                                    $.toast(res.Message);
                                    return false;
                                }
                                idx.close();
                                $.toast("编辑成功", 800, function () {
                                    getPigeonList();
                                })
                            });
                        });
                    }
                });
            });
        })

        /**
         * 删除成功
         * @param {type} msg
         * @returns {undefined}
         */
        $(document).on("click", ".del-user", function () {
            var id = $(this).attr("data-id");
            if (!id) {
                $.toast("编辑ID不能为空");
                return false;
            }

            var idx = $.alert("是否确定删除本用户", {
                "title": '删除用户',
                "ok": function () {
                    doDelUser(id, function (res) {
                        if (res.Code != 10000) {
                            $.toast(res.Message);
                            return false;
                        }
                        idx.close();
                        $.toast("删除成功", 800, function () {
                            getPigeonList();
                        })
                    });
                }
            });
        });

        /**
         * 获取用户列表
         * @param {type} callback
         * @returns {undefined}
         */
        function getPigeonList(callback) {
            if (typeof callback != "function") {
                callback = emptyFunc;
            }
            var data = getFormData();
            doGetUserList(data, function (res) {
                if (res.Code != 10000) {
                    showErr(res.Message);
                    $('#myList').hide();
                    callback(res);
                    return false;
                }

                $('#myList').show().html(res.Result.html);
                hideErr();
                callback(res);
            })
        }
        /**
         * 显示页面错误页面
         * @param msg
         */
        function showErr(msg) {
            $('.c-nodata').show().find('.msg').html(msg);
        }

        /**
         * 隐藏错误页面
         */
        function hideErr() {
            $('.c-nodata').hide().find('.msg').text('');
        }
    </script>
</block>

<block name="jsFile">

    <script type="text/javascript" src="__CST__/js/api.js?v={:C('JS_CSS_VERSION')}"></script>
    <script type="text/javascript" src="__CST__/js/user.api.js?v={:C('JS_CSS_VERSION')}"></script>
</block>

